/**
 * Mr.wa
 * Power In 2025 All Rights Reserved.
 */
package com.function.utils;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;

/**
 * oracle批处理查询util
 * oracle批量查询in关键词，每次只能查询1000条，需要循环多次查询
 *
 * @author Mr.wa * @version OracleBatchQueryUtil.java, v 0.1 2025-04-28 20:56 Mr.wa
 * @date 2025/04/28
 */
@Component
@Slf4j
public class OracleBatchQueryUtil {

    /**
     * 批量查询
     *
     * @param paramIdList 参数id列表
     * @param function    功能
     * @return {@link List }<{@link R }>
     */
    public static <T, R> List<R> batchQuery(List<T> paramIdList, Function<List<T>, List<R>> function) {
        List<R> resultList = new ArrayList<>();
        int count = paramIdList.size() % 1000 == 0 ? paramIdList.size() / 1000 : paramIdList.size() / 1000 + 1;
        for (int start = 0; start < count; start++) {
            int beginIndex = start * 1000;
            int endIndex = Math.min(beginIndex + 1000, paramIdList.size());
            log.info("start:{}, end:{}", beginIndex, endIndex);
            resultList.addAll(function.apply(paramIdList.subList(beginIndex, endIndex)));
        }
        return resultList;
    }

}